package ceha;

import ceha.ui.SystemTrayCde;
import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.UIManager;

public class CehaApp extends javax.swing.JFrame {

    public static String REPIMAGES = "C:/IMAGES/";
    public boolean adminstatus;
    private String requete;

    public CehaApp() {
        lookAndFeelManager();
        new SystemTrayCde(this);
        initComponents();


    }

    private void lookAndFeelManager() {
        //dossier des images
        try {
            // "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // UI modifiable    
        // new JLookAndFeelChooser(this).setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainpanel = new javax.swing.JPanel();
        connectPanel = new javax.swing.JPanel();
        pseudoField = new javax.swing.JTextField();
        passField = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        ValideButton = new javax.swing.JButton();
        annule = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        consoleField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        sortir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CEHA");
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.white);
        setIconImages(null);
        setResizable(false);

        mainpanel.setBackground(new java.awt.Color(255, 255, 255));
        mainpanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        connectPanel.setBackground(new java.awt.Color(0, 51, 153));

        pseudoField.setFont(new java.awt.Font("Tahoma", 1, 12));
        pseudoField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        passField.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Pseudo :");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Mot de passe :");

        ValideButton.setBackground(new java.awt.Color(255, 255, 255));
        ValideButton.setFont(new java.awt.Font("Times New Roman", 1, 18));
        ValideButton.setForeground(new java.awt.Color(0, 0, 255));
        ValideButton.setText("Indexation / Administration");
        ValideButton.setToolTipText("cliquez pour valider");
        ValideButton.setOpaque(false);
        ValideButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ValideButtonActionPerformed(evt);
            }
        });

        annule.setBackground(new java.awt.Color(255, 255, 255));
        annule.setFont(new java.awt.Font("Times New Roman", 1, 18));
        annule.setForeground(new java.awt.Color(0, 0, 255));
        annule.setText("Annuler");
        annule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 102, 255));

        consoleField.setBackground(new java.awt.Color(204, 255, 255));
        consoleField.setEditable(false);
        consoleField.setFont(new java.awt.Font("Tahoma", 1, 12));
        consoleField.setForeground(new java.awt.Color(255, 0, 0));
        consoleField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        consoleField.setAutoscrolls(false);
        consoleField.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "infos", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 3, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(consoleField, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(consoleField, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout connectPanelLayout = new javax.swing.GroupLayout(connectPanel);
        connectPanel.setLayout(connectPanelLayout);
        connectPanelLayout.setHorizontalGroup(
            connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(connectPanelLayout.createSequentialGroup()
                        .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pseudoField, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                            .addComponent(passField, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
                    .addGroup(connectPanelLayout.createSequentialGroup()
                        .addComponent(ValideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(annule)))
                .addContainerGap())
        );
        connectPanelLayout.setVerticalGroup(
            connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pseudoField, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ValideButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(annule, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("BASE DE DONNEES DU CEHA");

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ceha/ressource/logoCEHA.jpg"))); // NOI18N

        jLabel6.setBackground(new java.awt.Color(204, 0, 51));
        jLabel6.setFont(new java.awt.Font("Verdana", 3, 12));
        jLabel6.setForeground(new java.awt.Color(153, 0, 51));
        jLabel6.setText("Eteindre");

        sortir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ceha/ressource/EteindreOrdinateur.png"))); // NOI18N
        sortir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainpanelLayout = new javax.swing.GroupLayout(mainpanel);
        mainpanel.setLayout(mainpanelLayout);
        mainpanelLayout.setHorizontalGroup(
            mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainpanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addComponent(connectPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(sortir, 0, 0, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(24, 24, 24))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainpanelLayout.createSequentialGroup()
                .addContainerGap(178, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addContainerGap())
        );
        mainpanelLayout.setVerticalGroup(
            mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainpanelLayout.createSequentialGroup()
                .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(17, 17, 17)
                .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addComponent(connectPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainpanelLayout.createSequentialGroup()
                        .addComponent(sortir, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addGap(83, 83, 83))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-551)/2, (screenSize.height-400)/2, 551, 400);
    }// </editor-fold>//GEN-END:initComponents

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        pseudoField.setText("");
        passField.setText("");
        consoleField.setText("");
        connectPanel.setBackground(new Color(0, 51, 153));
    }//GEN-LAST:event_CancelActionPerformed

    private Connection MysqlConnect() {
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "ceha";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
        Connection conn = null;
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
//            consoleField.setText("Connection à a la base réussie , aucun login ");
        } catch (Exception e) {
            consoleField.setText("La base de donnée MySql n'est pas démarrée");
            e.printStackTrace();
        }

        return conn;
    }
    private void ValideButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ValideButtonActionPerformed

        String nom = pseudoField.getText();
        String mot = passField.getText();//deprecead TODO
        if (nom.equals("") && mot.equals("")) {
            consoleField.setText("Mode invité , Recherches uniquement");
            connectPanel.setBackground(Color.ORANGE);
            new RecherchePanel();
        } else {
            Connection conn = MysqlConnect();
            Statement stmt;
            ResultSet res = null;
            List<String> Lpseudo = new ArrayList<String>();
            List<String> Lpass = new ArrayList<String>();
            List<String> Lrole = new ArrayList<String>();
            try {
                stmt = conn.createStatement();
                res = stmt.executeQuery("SELECT * FROM membre");

                while (res.next()) {
                    Lpseudo.add(res.getString("pseudo"));
                    Lpass.add(res.getString("pass"));
                    Lrole.add(res.getString("role"));
                }
                res.close();
                stmt.close();
                conn.close();

            } catch (SQLException e) {
                consoleField.setText("Problème de connection");
                e.printStackTrace();
            }

            int no = 0;
            boolean auth = false;
            while (no < Lpseudo.size()) {
                if ((Lpseudo.get(no).equals(nom)) && (Lpass.get(no).equals(mot))) {
                    if (Lrole.get(no).equals("indexeur")) {
                        auth = true;
                        consoleField.setText("Bonjour " + nom + ":login accepté pour l'indexation");
                        connectPanel.setBackground(Color.green);
                        new IndexeurPanel(nom);
                        break;
                    } else if (Lrole.get(no).equals("admin")) {
                        auth = true;
                        consoleField.setText("Bonjour " + nom + ":login accepté pour l'administration");
                        connectPanel.setBackground(Color.green);
                        new AdminPanel(nom);
                        break;
                    }
                } else {
                    auth = false;
                    no++;
                }
                consoleField.setText("login impossible");
                connectPanel.setBackground(Color.red);

            }
        }

    }//GEN-LAST:event_ValideButtonActionPerformed

    private void OutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OutActionPerformed
        //TODO condtion de sortie, sauvegarde , logout
        System.exit(0);
    }//GEN-LAST:event_OutActionPerformed

    public static void main(String args[]) {

        new CehaApp().setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ValideButton;
    private javax.swing.JButton annule;
    private javax.swing.JPanel connectPanel;
    private javax.swing.JTextField consoleField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel mainpanel;
    private javax.swing.JPasswordField passField;
    private javax.swing.JTextField pseudoField;
    private javax.swing.JButton sortir;
    // End of variables declaration//GEN-END:variables
}
